package com.sgb.kjwl.view.ui.fragment.pick;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPickProjectListBinding;
import com.sgb.kjwl.di.pick.DaggerPickProjectComponent;
import com.sgb.kjwl.di.pick.PickProjectModule;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.pick.TenderDetailsActivity3;
import com.sgb.kjwl.view.ui.adapter.pick.PickHallBidingAdapter;
import com.sgb.kjwl.viewmodel.pick.PickPickingListFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * 招采企业-企业详情-招标中心
 *
 */
public class PickPickingListFragment extends AppFragment {
    public static final String TAG = "";
    private FragmentPickProjectListBinding binding;
    @Inject
    PickPickingListFragModel viewModel;
    private PickHallBidingAdapter pickingAdapter;
    private String id = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPickProjectComponent.builder().appComponent(appComponent).pickProjectModule(new PickProjectModule(this)).build().inject(this);
    }

    public static PickPickingListFragment newInstance(String id) {
        PickPickingListFragment fragment = new PickPickingListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_project_list, container, false);
        binding.setViewModel(viewModel);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();

        Bundle bundle =this.getArguments();//得到从Activity传来的数据
        if(bundle!=null){
            id = bundle.getString("id");
        }
        viewModel.getData(id);
    }

    private void initView() {
        if (pickingAdapter == null) {
            pickingAdapter = new PickHallBidingAdapter(getActivity(), null);
        }

        pickingAdapter.setOnItemClick(new AdapterOnItemClick<TenderListEntity>() {
            @Override
            public void onItemClick(TenderListEntity tenderListEntity, int position) {
                TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId() + "", 0);
            }
        });
        pickingAdapter.onIChildClick(new PickHallBidingAdapter.IChildClick() {
            @Override
            public void onClick(TenderListEntity tenderListEntity) {
                TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId() + "", 0);
            }
        });
        pickingAdapter.onIChildClick(new PickHallBidingAdapter.IChildClick() {
            @Override
            public void onClick(TenderListEntity tenderListEntity) {
                TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId() + "", 0);
            }
        });
        binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_list_divide));
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setAdapter(pickingAdapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getData(id);
                binding.recyclerView.setLoadingMoreEnabled(true);
            }

            @Override
            public void onLoadMore() {
                viewModel.getMoreData(id);
            }
        });
    }

    public void showData(List<TenderListEntity> pickingListEntities){
        pickingAdapter.setDatas(pickingListEntities);
        binding.recyclerView.refreshComplete();

        if (pickingListEntities.size()>0){
            binding.emptyView.setVisibility(View.GONE);
        }else {
            binding.emptyView.setVisibility(View.VISIBLE);
        }
    }

    public void showMoreData(List<TenderListEntity> pickingListEntities){
        pickingAdapter.addDatas(pickingListEntities);
        binding.recyclerView.loadMoreComplete();
    }

    public void fullScroll() {
        binding.recyclerView.getLayoutManager().scrollToPosition(0);
    }

    public void getUrl(String url, String id) {
        WebViewX5HostActivity.start(getActivity(), url, id);
    }
}
